package com.kitty.game.fight.artifact.active;


import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.util.FightMessageUtil;
import com.kitty.game.utils.StatusConst;

import java.util.List;

/**
 * 金刚圈
 * 为目标附加金刚圈状态，受到一定量的伤害后，可以反弹给对方最多5个目标
 */
class RingSkillProcessor extends ArtifactSkillProcessor {

    @Override
    public void binging(Fight fight, FightObject attacker,int specialSkillLevel) {
        int targetId = attacker.getTargetId();
        FightObject target = fight.queryFightObject(targetId);
        if (target == null) {
            return;
        }
        target.setStatus(StatusConst.STATUS_JINGANGQUAN, 4);
    }

    @Override
    public Object delayAction(Fight fight, FightObject attacker, Object... params) {
        FightObject target = (FightObject) params[0];
        int lastRound = target.getStatusValue(StatusConst.STATUS_JINGANGQUAN);
        if (lastRound <= 0) {
            return Boolean.FALSE;
        }
        target.setStatus(StatusConst.STATUS_JINGANGQUAN, lastRound - 1);

        int hurt = (int) params[1];
        int damageSum = target.getStatusScore(StatusConst.STATUS_JINGANGQUAN);
        damageSum += hurt;
        target.setStatusScore(StatusConst.STATUS_JINGANGQUAN, damageSum);
        int limit = 10000;
        if (damageSum <= limit) {
            return Boolean.FALSE;
        }
        List<FightObject> enemies = fight.getTargetSide(target.getId());
        if (enemies.size() <= 0) {
            return Boolean.FALSE;
        }
        List<FightObject> selected = enemies;
        if (selected.size() > 5) {
            selected = selected.subList(0, 4);
        }
        // 最多5个敌人，把累计的伤害平均给选中的列表
        int hurtPerEnemy = damageSum / selected.size();
        FightMessageService fightMessageService = SpringUtils.getFightMessageService();

        for (FightObject enemy : selected) {
            enemy.setCurrLife(enemy.getCurrLife() - hurtPerEnemy);
            FightMessageUtil.sendFightPackage(fight, fightMessageService.decreaseLife(target.getId(), enemy.getId(), -hurtPerEnemy, 0, 0));
            FightMessageUtil.sendFightPackage(fight, fightMessageService.updateTargetLife(target.getId(), target.getCurrLife()));
        }

        target.removeStatus(StatusConst.STATUS_JINGANGQUAN);

        return Boolean.TRUE;
    }

}